import router from "@/router/index";
export function getPageFormDataFn(obj: ISupAGetData, dataed: Ref<ISupRGetData>): Ref<ISupRGetData> {
  dataed.value = {
    superFormOpt: obj.superFormOpt,
    idRoute: router.currentRoute.value.params.id as string||obj.superFormOpt?.idRoute||'0',
    isSaving: false,
    formType: obj.formType,
    formEmitsObj: obj.formEmitsObj,
    formPropsObj: obj.formPropsObj,
    emit: obj.emit
  };
  return dataed;
}
export function getPageFormMethodsFn(data: Ref<ISupRGetFn>): ISupRGetFn {
  const methods: ISupOMethods = {
    backToListFn: () => {
      router.back();
    },
    judgeAddFn() {
      if (data.value.idRoute === "0") {
        if (data.value.formPropsObj.formParams) data.value.formRef?.resetfileFn(data.value.formPropsObj.formParams);
      } else {
        this.getDetailFn();
      }
    },
    //获取详情
    getDetailFn: async () => {
      if (!data.value.superFormOpt?.detailUrl) return;
      const res = await hfoRequest.get({ url: data.value.superFormOpt?.detailUrl + `/${data.value.idRoute}` });
      if (res) {
        data.value.emit("emitGetDetailHandle", res.data);
        const obj = res.data;
        data.value.formRef?.resetfileFn(obj);
        if (obj.idRoute) data.value.formRef.form.id = obj.idRoute;
      }
    },
    async submitFormFn() {
      try {
        data.value.isSaving = true;
        const res = await data.value.formRef?.submitFormFn();
        data.value.emit("emitSaveAfter", {
          res: res
        });
        data.value.isSaving = false;
      } catch (error) {
        data.value.isSaving = false;
      }
    },
    initialFn() {
      data.value.emit("emitChangeProps", { e: true, propsName: ["formOpt", "otherBtn"] });
    },
    resetFn() {
      data.value.formRef?.resetfileFn(data.value.formPropsObj.formParams);
    }
  };
  nextTick(() => {
    data.value.formRef?.resetfileFn(data.value.formPropsObj.formParams);
  });
  methods.initialFn();
  methods.judgeAddFn();
  data.value.methods = methods;
  return data.value;
}
